package org.shock.ss.sms.action.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.commons.utils.Util;

import org.shock.ss.sms.bean.Tree;
import org.shock.ss.sms.bean.system.Role;

import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.impl.RoleDaoImpl;

import org.shock.ss.sms.service.system.RoleService;
import org.shock.ss.sms.service.system.impl.RoleServiceImpl;
import org.shock.ss.sms.action.BaseAction;

/**
 * 角色管理action
 * 
 * @author jile
 * 
 */
public class RoleAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	/** 获得service */
	private RoleService rs = new RoleServiceImpl((RoleDaoImpl) DaoFactory
			.getDao("RoleDao"));
	private Role role = new Role();
	private List list = new ArrayList();
	private Integer node;
	private String op = "";// 操作方式普通请求 或ajax请求
	private String userids;
	private String ids;

	public String execute() throws Exception {
		return SUCCESS;
	}

	public String viewRole() throws Exception {
		return SUCCESS;
	}

	/**
	 * 增加或更新角色 根据bean 的id进行判断，如果id为null则为增加，否则为更新。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveRole() throws Exception {
		if (role.getId() == null) {
			addRole();
		} else {
			updateRole();
		}
		if ("ajax".equals(op)) {
			return "ajxsuccess";
		}
		return SUCCESS;
	}

	@RightAnno( rightNote = "角色添加")
	public String addRole() throws Exception {
		log.debug(ReflexUtils.rwritertoString(role));// 调试bean信息
		if (action.equals("before")) {// 显示添加页面
			return "before";
		}
		rs.add(role);
		message = "添加成功！";
		jmpUrl = "listRole.do";
		return SUCCESS;
	}

	@RightAnno( rightNote = "修改角色")
	public String updateRole() throws Exception {
		if (action.equals("before")) {// 显示添加页面
			String sql = "select * from "+Role.getTable()+" where id=" + role.getId();
			BeanUtils.populate(role, rs.view(sql));
			return "before";
		}
		rs.update(role);
		message = "更新成功！";
		jmpUrl = "listRole.do";
		return SUCCESS;
	}

	@RightAnno(rightNote = "角色列表")
	public String listRole() throws Exception {
		/** 查询条件 */
		if (null != role) {
			if (!Util.isEmpty(role.getName())) {
				sqladd.append("and name like '%" + role.getName() + "%'");
			}
			if (!Util.isEmpty(role.getCode())) {
				sqladd.append(" and code like '%" + role.getCode() + "%'");
			}
		}
		Pager pager = new Pager();
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		String sqlcount = "select count(1) from "+Role.getTable()+" where 1=1 " + sqladd;
		String sql = "select * from "+Role.getTable()+" where 1=1 " + sqladd;
		//System.out.println(sql);
		pager = rs.list("asc", "paixu,id", pageno, rownum, sqlcount, sql);
		list = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	@RightAnno( rightNote = "删除角色")
	public String delRole() throws Exception {
		String sql = "delete from "+Role.getTable()+" where id =" + role.getId();
		rs.del(sql);
		message = "删除成功！";
		jmpUrl = "listRole.do";
		return SUCCESS;
	}

	/** 角色授权页面 */
	@RightAnno( rightNote = "角色授权")
	public String roleRight() throws Exception {
		if ("before".equals(action)) {
			String sql = "select * from "+Role.getTable()+" where id=" + role.getId();
			Map map = rs.view(sql);
			BeanUtils.populate(role, map);
			return "before";
		}
		rs.saveRoleRights(role);
		if ("ajax".equals(op)) {
			return "ajxsuccess";
		}
		return SUCCESS;
	}

	/** 为角色添加人员 */
	@RightAnno( rightNote = "角色人员")
	public String roleUser() throws Exception {
		if ("before".equals(action)) {
			String sql = "select * from "+Role.getTable()+" where id=" + role.getId();
			Map map = rs.view(sql);
			BeanUtils.populate(role, map);
			return "before";
		}
		if (null == userids || "".equals(userids))
			userids = "0";
		if (null == ids || "".equals(ids))
			ids = "0";
		rs.saveRoleUsers(role.getId(), userids, ids);
		if ("ajax".equals(op)) {
			return "ajxsuccess";
		}
		return SUCCESS;
	}

	/** 角色绑定桌面 */
	@RightAnno(rightNote = "角色绑定桌面")
	public String rolePortal() throws Exception {
		if ("before".equals(action)) {
			String sql = "select * from "+Role.getTable()+" where id=" + role.getId();
			Map map = rs.view(sql);
			BeanUtils.populate(role, map);
			return "before";
		}
		rs.saveRolePortals(role);
		if ("ajax".equals(op)) {
			return "ajxsuccess";
		}
		return SUCCESS;
	}
	/** 角色绑定区域 */
	@RightAnno(rightNote = "角色绑定区域")
	public String roleArea() throws Exception {
		if ("before".equals(action)) {
			String sql = "select * from "+Role.getTable()+" where id=" + role.getId();
			Map map = rs.view(sql);
			BeanUtils.populate(role, map);
			return "before";
		}
		rs.saveRoleArea(role);
		if ("ajax".equals(op)) {
			return "ajxsuccess";
		}
		return SUCCESS;
	}
	/** 用于产生角色选择树，供Ext选择 */
	public String genRoleTree() throws Exception {
		String sql = "select name,id from "+Role.getTable()+" order by paixu";
		list = DbTools.getMapsInList(sql);
		List tmpList = new ArrayList();
		for (Map map : (ArrayList<Map>) list) {
			Tree tree = new Tree();
			tree.setId((String) map.get("id"));
			tree.setText((String) map.get("name"));
			tree.setLeaf(true);
			tree.setChecked(false);
			tmpList.add(tree);
		}
		jsonString = JSONArray.fromObject(tmpList).toString();
		//System.out.println(jsonString);
		return SUCCESS;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Integer getNode() {
		return node;
	}

	public void setNode(Integer node) {
		this.node = node;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public String getUserids() {
		return userids;
	}

	public void setUserids(String userids) {
		this.userids = userids;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
